<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/27
 * Time: 16:30
 * 区域门禁管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;

class SlidingDoor extends OperationBase {

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 区域门禁设备
     */
    public function index() {
        $auth_group = sp_get_auth_menu_3("20041001");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        echo $this->fetch();
    }

    /**
     * 区域门禁设备数据
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function ajaxIndex() {
        $name = input("name/s");
        $shop_id = input("shop_id/d");
        $draw = input("draw",0) + 1;//请求时间
        $where = array();
        if (!empty($name)) {
            $where["sd.name"] = array("like", "%{$name}%");
        }
        if (!empty($shop_id)) {
            $where["sd.shop_id"] = array("eq", $shop_id);
        }
        $total = db("sliding_door")->alias("sd")->where($where)->count(1);
        $list = db("sliding_door")->alias("sd")
            ->field("sd.*, s.shop_name")
            ->join("m_shop s", "s.id = sd.shop_id", "left")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();
        $this->ajax(array('pageData'=>$list,'total'=>$total,"draw"=>$draw));
    }

    /**
     * 编辑
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function edit() {
        $id  = input("id/d");
        if (empty($id)) {
            $this->ajax_error("参数错误");
        }
        $sliding_door = db("sliding_door")->find($id);
        if (empty($sliding_door)) {
            $this->ajax_error("参数错误");
        }
        $sliding_door["shop_name"] = db("shop")->where(array("id" => array("eq", $sliding_door["shop_id"])))->value("shop_name");
        $this->assign("model", $sliding_door);
        echo $this->fetch();
    }

    /**
     * 编辑操作
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    function editPost(){
        if(request()->isPost()){//提交
            $id = input("id/d");
            $name = input("name/s");
            $desc = input("desc/s");
            if (empty($id)) {
                $this->ajax_error("参数错误");
            }
            $sliding_door = db("sliding_door")->find($id);
            if (empty($sliding_door)) {
                $this->ajax_error("参数错误");
            }
            $update_res = db("sliding_door")->update(array("id" => $id, "name" => $name, "desc" => $desc));
            if ($update_res === false) {
                $this->ajax_error("失败");
            }
            $this->ajax_success("成功");
        }
    }


    /**
     * 添加
     */
    function add() {
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        echo $this->fetch();
    }

    /***
     * 添加操作
     */
    function addPost(){
        if(request()->isPost()){
            $name = input("name/s");
            $code = input("code/s");
            $desc = input("desc/s");
            $shop_id = input("shop_id/d");
            if (empty($name)) {
                $this->ajax_error("名称不能为空");
            }
            if (empty($code)) {
                $this->ajax_error("滑门权限值不能为空");
            }
            if (empty($desc)) {
                $this->ajax_error("描述不能为空");
            }
            if (empty($shop_id)) {
                $this->ajax_error("所属店铺不能为空");
            }
            $hexdec_code = hexdec ($code); //十六进制转化成十进制
            //判断是否为2的n次方
            $flag = ($hexdec_code > 1) && (($hexdec_code & ($hexdec_code - 1)) == 0);
            if (!$flag) {
                $this->ajax_error("滑门权限值不为2的N次方");
            }
            $is_exist = db("sliding_door")->where(array("code" => $code, "shop_id" => $shop_id))->count(1);
            if ($is_exist > 0) {
                $this->ajax_error("编号已经存在");
            }else{
                $add_res = db("sliding_door")->insert(array("name" => $name, "desc" => $desc, "code" => $code, 'shop_id' => $shop_id));
                if (empty($add_res)) {
                    $this->ajax_error("失败");
                }
                $this->ajax_success("成功");
            }
        }
    }

    /**
     * 删除区域门禁
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    function delete(){
        $id = input("id");
        $where["door_ids"] = array("like", "%{$id}%");
        $is_exist = db("sliding_group")->where($where)->count(1);
        if ($is_exist > 0) {//区域门禁已分配
            $this->ajax_error("区域门禁在权限分组已使用，无法删除");
        }
        $delete_res = db("sliding_door")->where(array("id" => $id))->delete();
        if (empty($delete_res)) {
            $this->error("删除失败");
        }
        $this->ajax_success("成功");
    }

    /**
     * 门禁记录
     */
    public function memberEnter()
    {
        $auth_group = sp_get_auth_menu_3("20041000");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        $this->assign("now", date("Y-m-d"));
        echo $this->fetch();
    }

    /**
     * 入场记录数据
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function ajaxEnter() {

        $this->ajax($this->getMemberEnterData());
    }

    /**
     * 导出excle
     * */
    public function exportExcel(){
        $pageSize = input("pageSize");
        if (empty($pageSize)) {
            exit();
        }

        $titles_des = array("卡号", "姓名", "卡类型", "创建时间", "错误代码", "归属店铺", "店铺id", "接口版本");
        $titles_field = array("card_no", "name", "owner_type", "create_time", "error_msg", "shop_name", "shop_id", "api_version");
        $data = $this->getMemberEnterData();
        $list = $data['pageData'];
        foreach ($list as $key => $value) {
            if($value['owner_type']==1){
                $list[$key]['owner_type']="会员卡";
            }else if($value['owner_type']==2){
                $list[$key]['owner_type']="员工卡";
            }else if($value['owner_type']==3){
                $list[$key]['owner_type']="临时卡";
            }
        }

        $title = "区域门禁日志";
        var_dump(export_excel($titles_des, $titles_field, $list, $title));
        exit();
    }

    /**
     * 获取临时借记卡入场记录数据
     */
    private function getMemberEnterData()
    {

        $name = input("name/s");
        $mobile = input("mobile/s");
        $card_no = input("card_no/s");
        $shop_id = input("shop_id/d");
        $draw = input("draw",0) + 1;//请求时间

        $where = array();
        if (!empty($name)) {
            $where["sl.name"] = array("like", "%{$name}%");
        }
        if (!empty($mobile)) {
            $where["sl.mobile"] = array("like", "%{$mobile}%");
        }
        if (!empty($card_no)) {
            $where["sl.card_no"] = array("like", "%{$card_no}%");
        }

        if (!empty($shop_id)) {
            $where["sl.shop_id"] = array("eq", $shop_id);
        }

        $total = db("sliding_log")->alias("sl")->where($where)->count(1);
        $list = db("sliding_log")->alias("sl")
            ->join("m_shop s", "s.id = sl.shop_id", "left")
            ->field("sl.*, s.shop_name, s.api_version")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();

        return array('pageData' => $list, 'total' => $total, "draw" => $draw);
    }
}